<?php

/**
 * searchEngine actions.
 *
 * @package    smp-crm
 * @subpackage searchEngine
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class searchEngineActions extends sfActions {

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $this->forward('default', 'module');
    }

    public function executeSearch(sfWebRequest $request) {
        $query = $request->getParameter('query');
        $val_query = explode(' ', $query);
        $results = Doctrine_Core::getTable('sfGuardUser')->createQuery('u')
                            ->where('u.first_name LIKE ?', "%$val_query[0]%")
                            ->orWhere('u.last_name LIKE ?', "%$val_query[0]%");
        
        for($i=1; $i < count($val_query); $i++) {
           $results->orWhere('u.first_name LIKE ?', "%$val_query[$i]%") 
                   ->orWhere('u.last_name LIKE ?', "%$val_query[$i]%");
        }   

        $this->results = $results->execute();
        
        if ($request->isXmlHttpRequest()) {
            if ('*' == $query || !$this->results) {
                return $this->renderText('No results.');
            }
            return $this->renderPartial('searchEngine/listAjax', array('results' => $this->results));
        }
    }

}
